package com.yummy.controller;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.yummy.common.BaseResponse;
import com.yummy.dto.Alipay;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;

/**
 * @author yinbo
 */

@RestController
@RequestMapping("/alipay")
public class AlipayController {

	@RequestMapping("/pay")
	public String alipay(Alipay aliPay) {

		System.out.println("alipay==>"+aliPay);

		AlipayTradePagePayResponse response;
		try {
			//  发起API调用（以创建当面付收款二维码为例）
			response = Factory.Payment.Page()
					.pay(URLEncoder.encode(aliPay.getSubject(), "UTF-8"), aliPay.getTraceNo(), aliPay.getTotalAmount(), "");
		} catch (Exception e) {
			System.err.println("调用遭遇异常，原因：" + e.getMessage());
			throw new RuntimeException(e.getMessage(), e);
		}
		return response.getBody();
	}
}
